package com.heima.common.tess4j;

import lombok.Getter;
import lombok.Setter;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.awt.image.BufferedImage;
@Setter
@Getter
@Component
@ConfigurationProperties(prefix = "tess4j")
public class Tess4jClient {
    private String dataPath;
    private String language;

    public String doOCR(BufferedImage image) throws TesseractException {
        // 初始化tess4j对象
        Tesseract tesseract = new Tesseract();
        // 设置字体库所在路径
        tesseract.setDatapath(dataPath);
        // 设置语言类型
        tesseract.setLanguage(language);
        // 开始识别
        String result = tesseract.doOCR(image);
        result = result.replaceAll("\\r|\\n","-").replaceAll(" ","");
        return result;
    }
}
